
<?php
ini_set('display_errors',0); 
define('isdollars', 1);
define('maxusers',10);


if(isdollars == 0){
	define('currency', '&#8360; ');
	define('sign_up_amount',  50);
	define('game_amount', 200);
}else{
	define('isdollars', 1);
	define('currency', '$');
	define('sign_up_amount',  5);
	define('game_amount', 10);
}
 error_reporting(E_ALL);
function connectdb(){
        $dbhost = 'localhost';
        $dbuser = 'facebook';
        $dbpass = 'facebook';
        $dbname = 'jmj_test';
        $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
        mysql_select_db($dbname);
        return $conn;
}

function closedb($conn){
	if($conn != NULL)
        mysql_close($conn);
}

?>

<?php
//gets friends status
function get_fstatus($access_token){
    // Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+status_id,+uid+,+message,time+FROM+status+WHERE+uid+IN+(SELECT+uid2+FROM+friend+WHERE+uid1+=+me())'
    . '&access_token=' . $access_token;
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  //display results of fql query
  echo '<pre>';
  //print_r("query results:");
  //print_r($fql_query_obj);
  echo '</pre>';
  return $fql_query_obj;
}
function get_comments($access_token){
    // Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+id,+fromid,+time,+post_fbid,+text+,object_id+FROM+comment+WHERE+object_id+IN+(SELECT+status_id+FROM+status+WHERE+uid+=+me())'
    . '&access_token=' . $access_token;
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  //display results of fql query
  echo '<pre>';
  //print_r("query results:");
  //print_r($fql_query_obj);
  echo '</pre>';
  return $fql_query_obj;
}
function get_comment_post($access_token){
// Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+id,+fromid,+time,+post_fbid,+text+,object_id+FROM+comment+WHERE+object_id+IN+(SELECT+substr(substr(post_id,(strpos(post_id,"_")),(strlen(post_id)-strpos(post_id,"_"))),1,strlen(substr(post_id,(strpos(post_id,"_")-1),(strlen(post_id)-strpos(post_id,"_")))))+FROM+stream+WHERE+source_id=me()+AND+type=56+LIMIT+50000)+OR+object_id+IN+(SELECT+substr(substr(post_id,(strpos(post_id,"_")),(strlen(post_id)-strpos(post_id,"_"))),1,strlen(substr(post_id,(strpos(post_id,"_")-1),(strlen(post_id)-strpos(post_id,"_")))))+FROM+stream+WHERE+source_id=me()+AND+message>1+AND+type<0+AND+description<0+LIMIT+50000)'
    . '&access_token=' . $access_token;
    
    
    //SELECT+post_id+FROM+stream+WHERE+source_id=me()+AND+message>1+AND+type<0+AND+description<0+LIMIT+50000) -- gets others post on users wall. The post id is not same as objectid of comment
    //SELECT+post_id+FROM+stream+WHERE+source_id=me()+AND+type=56+LIMIT+50000 --gets users post on others wall
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  //display results of fql query
  echo '<pre>';
  //print_r("query results:");
  //print_r($fql_query_obj);
  echo '</pre>';
  return $fql_query_obj;

}
function get_comments_photo($access_token){
    // Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+id,+fromid,+time,+post_fbid,+text+,object_id+FROM+comment+WHERE+object_id+IN+(SELECT+object_id+FROM+photo+WHERE+owner+=+me())'
    . '&access_token=' . $access_token;
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  //display results of fql query
  echo '<pre>';
  //print_r("query results:");
  //print_r($fql_query_obj);
  echo '</pre>';
  return $fql_query_obj;
}
function get_comments_album($access_token){
    // Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+id,+fromid,+time,+post_fbid,+text+,object_id+FROM+comment+WHERE+object_id+IN+(SELECT+object_id+FROM+album+WHERE+owner+=+me())'
    . '&access_token=' . $access_token;
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  //display results of fql query
  echo '<pre>';
  //print_r("query results:");
  //print_r($fql_query_obj);
  echo '</pre>';
  return $fql_query_obj;
}

//get user id
function get_uid($access_token){
    // Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+uid+FROM+user+WHERE+uid=me()'
    . '&access_token=' . $access_token;
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  //display results of fql query
  echo '<pre>';
  //print_r("query results:");
  //print_r($fql_query_obj);
  echo '</pre>';
  return $fql_query_obj;
}



function get_uid_only($access_token){
    // Run fql query
	$temp = get_uid($access_token);
	foreach($temp['data'] as $status)
		{
			$user_id=$status['uid'];
		}
	return $user_id;
}

//get status id
function get_status_id($access_token){
    // Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+status_id+FROM+status+WHERE+uid=me()'
    . '&access_token=' . $access_token;
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  //display results of fql query
  echo '<pre>';
  //print_r("query results:");
  //print_r($fql_query_obj);
  echo '</pre>';
  return $fql_query_obj;
}
//get user details to populate user table
function get_user($access_token){
    // Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+uid+,name+,birthday_date+,sex+,current_location,email+FROM+user+WHERE+uid=me()'
    . '&access_token=' . $access_token;
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  //display results of fql query
  echo '<pre>';
  //print_r("query results:");
  //print_r($fql_query_obj);
  echo '</pre>';
  return $fql_query_obj;
}

//gets statusid, message and uid of the user
function get_status_message($access_token){
    // Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+message+,+status_id+,+uid+,time+FROM+status+WHERE+uid=me()+LIMIT+5000'
    . '&access_token=' . $access_token;
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  //display results of fql query
  echo '<pre>';
  //print_r("query results:");
  //print_r($fql_query_obj);
  echo '</pre>';
  return $fql_query_obj;
}
//gets statusid, message and uid of the user
function get_status_message_stream($access_token){
    // Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+post_id+,message+,actor_id+,updated_time+,description+,impressions+FROM+stream+WHERE+source_id=me()AND+created_time+>=1262196000+LIMIT+50000'
    . '&access_token=' . $access_token;
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  //display results of fql query
  echo '<pre>';
  //print_r("query results:");
  print_r($fql_query_obj);
  echo '</pre>';
  return $fql_query_obj;
}

//populates friends table
function get_friends($access_token){
    // Run fql query 
    $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+uid,name+FROM+user+WHERE+uid+IN+(SELECT+uid2+FROM+friend+WHERE+uid1+=+me())'
    . '&access_token=' . $access_token;
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  //display results of fql query
  echo '<pre>';
  //print_r("query results:");
  //print_r($fql_query_obj);
  echo '</pre>';
  return $fql_query_obj;}
  
  //populates photo table
function get_photo($access_token){
    // Run fql query 
    $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+pid+,object_id+,album_object_id+,owner+FROM+photo+WHERE+owner+=+me()'
    . '&access_token=' . $access_token;
     // First logically check the result, TRUE: Result is passed to $fql_query_result.
  //                                   FALSE: $flq_query_result = 0 or false.
  $fql_query_result = file_get_contents($fql_query_url)?file_get_contents($fql_query_url):0;
  // Only json_decode the result if it was valid.
  $fql_query_obj = (!$fql_query_result == 0) ? json_decode($fql_query_result, true) : "FQL was not a valid query";

  //display results of fql query
  echo '<pre>';
  //print_r("query results:");
  //print_r($fql_query_obj);
  echo '</pre>';
  return $fql_query_obj;}
//get likes on status message
function get_like($access_token){
    // Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+user_id,+object_id,+post_id,+object_type+FROM+like+WHERE+object_id+IN+(SELECT+status_id+FROM+status+WHERE+uid+=+me())'
    . '&access_token=' . $access_token;
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  //display results of fql query
  echo '<pre>';
  //print_r("query results:");
  //print_r($fql_query_obj);
  echo '</pre>';
  return $fql_query_obj;
}
//get likes on photo
function get_like_photo($access_token){
    // Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+user_id,+object_id,+post_id,+object_type+FROM+like+WHERE+object_id+IN+(SELECT+object_id+FROM+photo+WHERE+owner+=+me())'
    . '&access_token=' . $access_token;
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  //display results of fql query
  echo '<pre>';
  //print_r("query results:");
  //print_r($fql_query_obj);
  echo '</pre>';
  return $fql_query_obj;
}
function get_like_comment($access_token){
    // Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+user_id,+object_id,+post_id,+object_type+FROM+like+WHERE+object_id+IN+(SELECT+post_fbid+FROM+comment+WHERE+object_id+IN+(SELECT+object_id+FROM+album+WHERE+owner+=+me())+OR+object_id+IN+(SELECT+object_id+FROM+photo+WHERE+owner+=+me())+OR+object_id+IN+(SELECT+status_id+FROM+status+WHERE+uid+=+me()))'
    . '&access_token=' . $access_token;
    
    
    //SELECT+post_id+FROM+stream+WHERE+source_id=me()+AND+message>1+AND+type<0+AND+description<0+LIMIT+50000) -- gets others post on users wall. The post id is not same as objectid of comment
    //SELECT+post_id+FROM+stream+WHERE+source_id=me()+AND+type=56+LIMIT+50000 --gets users post on others wall
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  //display results of fql query
  echo '<pre>';
  //print_r("query results:");
  //print_r($fql_query_obj);
  echo '</pre>';
  return $fql_query_obj;
}

//get likes on album
function get_like_album($access_token){
    // Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+user_id,+object_id,+post_id,+object_type+FROM+like+WHERE+object_id+IN+(SELECT+object_id+FROM+album+WHERE+owner+=+me())'
    . '&access_token=' . $access_token;
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  //display results of fql query
  echo '<pre>';
  //print_r("query results:");
  //print_r($fql_query_obj);
  echo '</pre>';
  return $fql_query_obj;
}
function get_like_det($access_token){
    // Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+status_id,+message+FROM+status+WHERE+status_id+=10200105233852500'
    . '&access_token=' . $access_token;
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  //display results of fql query
  echo '<pre>';
  //print_r("query results:");
  print_r($fql_query_obj);
  echo '</pre>';
  return $fql_query_obj;
} 
//get THE PAGES A USER LIKES    
function get_page_like($access_token){
    // Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+page_id,+name,+description,+type,+categories+FROM+page+WHERE+page_id+IN+(SELECT+page_id+FROM+page_fan+WHERE+uid+=+me())'
    . '&access_token=' . $access_token;
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  //display results of fql query
  echo '<pre>';
  //print_r("query results:");
  //print_r($fql_query_obj);
  echo '</pre>';
  return $fql_query_obj;
}
/*
//get groups the user is a part of. This table does not have any results . Several people have reported this!   
function get_fb_group($access_token){
    // Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+gid+,uid+FROM+group_member+WHERE+gid+=+121938571175971+and+uid+=1199808427'
    . '&access_token=' . $access_token;
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  //display results of fql query
  echo '<pre>';
  //print_r("query results:");
  print_r($fql_query_obj);
  echo '</pre>';
  return $fql_query_obj;
}
*/
// this is retrieve posts posted on the users wall by a friend
function get_post($access_token){
    // Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+message+,+description+,+description_tags+,post_id+,+type+FROM+stream+WHERE+source_id=me()+AND+type=56+LIMIT+50000'
    . '&access_token=' . $access_token;
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);
// type=56 is when the user posts on someoneelse wall
  //display results of fql query
  echo '<pre>';
  //print_r("query results:");
  //print_r($fql_query_obj);
  echo '</pre>';
  return $fql_query_obj;
}

?>  
<?php
    require_once("./config.php");
    $me = $facebook->api('/me');
    $access_token = $facebook->getAccessToken();
    $user = $facebook->getUser();

 
?>
<?php
function insert_user($access_token)
{
    $result=get_user($access_token);
    //print_r($result);
    if ($con=connectdb())
    {
    if(!empty($result['data'])) 
        {
          foreach($result['data'] as $status)
          {
        
          $uid = $status['uid'];
          $Name=$status['name'];
	   $email=$status['email'];
          $Age=$status['birthday_date'];
          if ($status['sex']=="female")
          {
          $Gender='F';
          }
          elseif ($status['sex']=="male")
          {
          $Gender='M';
          }
          else
          {
          $Gender='';
          }
          if ($Age)
          {
          $delimiter="/";
          $val= explode ( $delimiter,$Age );
          $Age=date("Y")-$val[2];
          }
          $Location=$status['current_location']['name'];
          $query = "INSERT INTO user (UID,Name,Gender,Location,Age,email) VALUES('{$uid}','{$Name}','{$Gender}','{$Location}','{$Age}','{$email}')";
	   $result1 = mysql_query($query, $con);
          if ($result1)
            {
            //echo "<br/>JMJ Success User";
            }
          }
        }
    }
}
function insert_friends($access_token)
{
    $result1=get_friends($access_token);
    $result2=get_uid($access_token);
    foreach($result2['data'] as $status)
    {
    $uid=$status['uid'];
    }
    //echo $uid."<br/>";
   if ($con=connectdb())
    {
     if(!empty($result1['data'])) 
        {
          foreach($result1['data'] as $status)
          {
        
          $fuid = $status['uid'];
          $Name=$status['name'];
          $query = "INSERT INTO friends (UID,FID,FName) VALUES('{$uid}','{$fuid}','{$Name}')";
           //echo $query ;
          $result3 = mysql_query($query, $con);
          if ($result3)
            {
            //echo "<br/>JMJ Success friends";
            }
          }
        }
    }
}
function insert_status($access_token,$des)
{
    if ($des==1)
    {
    $result1=get_status_message($access_token);
    }
    elseif ($des==2)
    {
    $result1=get_fstatus($access_token);
    }
   if ($con=connectdb())
    {
     if(!empty($result1['data'])) 
        {
          foreach($result1['data'] as $status)
          {
        
          $Status_id = $status['status_id'];
          $UID =$status['uid'];
          $Status_message =$status['message'];
          
          $iso8601 = date('c', $status ['time']);
          $Updated_time=$iso8601;
          $query = "INSERT INTO status (Status_id,UID,Status_message,Updated_time) VALUES('{$Status_id}','{$UID}','{$Status_message}','{$Updated_time}')";
           //echo $query ;
          $result3 = mysql_query($query, $con);
          if ($result3)
            {
            //echo "<br/>JMJ Success Status";
            }
          }
        }
    }
    closedb($con);
}
function insert_comment($access_token,$des)
{
    if ($des==1)
    {
    $result1=get_comments($access_token);
    $type='status';
    }
    elseif($des==2)
    {
        $result1=get_comments_photo($access_token);
         $type='photo';
    }
    
    elseif($des==3)
    {
        $result1=get_comments_album($access_token);
         $type='album';
    }
    elseif($des==4)
    {
        $result1=get_comment_post($access_token);
         $type='album';
    }
    $result2=get_uid($access_token);
    foreach($result2['data'] as $status)
    {
    $uid=$status['uid'];
    }
    
    if ($con=connectdb())
    {
     if(!empty($result1['data'])) 
        {
          foreach($result1['data'] as $status)
          {
        
          $Fromid = $status['fromid'];
          $comment_text  =$status['text'];
          $object_id  =$status['object_id'];
          $post_fbid  =$status['post_fbid'];
           $iso8601 = date('c', $status ['time']);
           $Updated_time=$iso8601;
          $query = "INSERT INTO comment (Fromid,comment_text,object_id,post_fbid,UID,type,Updated_time) VALUES('{$Fromid}','{$comment_text}','{$object_id}','{$post_fbid}','{$uid}','{$type}','{$Updated_time}')";
        //echo $query ;
          $result3 = mysql_query($query, $con);
          if ($result3)
            {
            //echo "<br/>JMJ Success comment";
            }
          }
        }
    }
}
function insert_page_like($access_token)
{
if ($con=connectdb())
    {
    $result2=get_uid($access_token);
    foreach($result2['data'] as $status)
    {
        $uid=$status['uid'];
    }
    $page_like= get_page_like($access_token); 
    
     if(!empty($page_like['data'])) 
        {
	  
          foreach($page_like['data'] as $status)
          {
          
        $page_id = $status['page_id'];
        $page_name = $status['name'];
        $description =$status['description'];
        $type =$status['type'];
        if(!empty($status['categories']))
        {
            foreach($status['categories'] as $swith)
            {
                $cat_name =$swith['name'];
                $cat_id=$swith['id'];
                $query = "INSERT INTO page_like (page_id,page_name,description,type,cat_name,cat_id,UID) VALUES('{$page_id }','{$page_name}', '{$description}','{$type}','{$cat_name}','{$cat_id}','{$uid}')";
                //echo  $query ."<br/>";
		 $result3 = mysql_query($query, $con);
		
            }
        }
        else
        {
                $cat_name =null;
                $cat_id=null;
                $query = "INSERT INTO page_like (page_id,page_name,description,type,cat_name,cat_id,UID) VALUES('{$page_id }','{$page_name}', '{$description}','{$type}','{$cat_name}','{$cat_id}','{$uid}')";
                //echo  $query ."<br/>";
		 $result3 = mysql_query($query, $con);


        }
            
            

          }
        }
    }
}

//this function retirieves likes on comments made on posts by friends on the users wall and on comments made on users posts on friends wall
function get_like_comment_post($access_token){
    // Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+user_id,+object_id,+post_id,+object_type+FROM+like+WHERE+object_id+IN+(SELECT+post_fbid+FROM+comment+WHERE+object_id+IN+(SELECT+substr(substr(post_id,(strpos(post_id,"_")),(strlen(post_id)-strpos(post_id,"_"))),1,strlen(substr(post_id,(strpos(post_id,"_")-1),(strlen(post_id)-strpos(post_id,"_")))))+FROM+stream+WHERE+source_id=me()+AND+type=56+LIMIT+50000)+OR+object_id+IN+(SELECT+substr(substr(post_id,(strpos(post_id,"_")),(strlen(post_id)-strpos(post_id,"_"))),1,strlen(substr(post_id,(strpos(post_id,"_")-1),(strlen(post_id)-strpos(post_id,"_")))))+FROM+stream+WHERE+source_id=me()+AND+message>1+AND+type<0+AND+description<0+LIMIT+50000))'
    . '&access_token=' . $access_token;
    
    
    //SELECT+post_id+FROM+stream+WHERE+source_id=me()+AND+message>1+AND+type<0+AND+description<0+LIMIT+50000) -- gets others post on users wall. The post id is not same as objectid of comment
    //SELECT+post_id+FROM+stream+WHERE+source_id=me()+AND+type=56+LIMIT+50000 --gets users post on others wall
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  //display results of fql query
  echo '<pre>';
  //print_r("query results:");
  //print_r($fql_query_obj);
  echo '</pre>';
  return $fql_query_obj;
}
function insert_like($access_token,$des)
{
    
    if ($des==1)
    {
    $result1=get_like($access_token);
    $type='status';
    }
    elseif($des==2)
    {
        $result1=get_like_photo($access_token);
         $type='photo';
    }
    
    elseif($des==3)
    {
        $result1=get_like_album($access_token);
         $type='album';
    }
    elseif($des==4)
    {
        $result1=get_like_comment($access_token);
         $type='comment';
    }
    elseif($des==5)
    {
        $result1=get_like_comment_post($access_token);
        $type='comment on posts by other users';
    }
    $result2=get_uid($access_token);
    foreach($result2['data'] as $status)
    {
    $uid=$status['uid'];
    }
    
    if ($con=connectdb())
    {
     if(!empty($result1['data'])) 
        {
          
          foreach($result1['data'] as $status)
          {
        
          $Fromid = $status['user_id'];
          $object_id  =$status['object_id'];
          if (!empty($status['post_fbid']))
          {
          $post_fbid  =$status['post_fbid'];
          }
          else
          {
              $post_fbid=null;
          }
           
          $query = "INSERT INTO likes (Fromid,object_id,post_fbid,UID,type) VALUES('{$Fromid}','{$object_id}','{$post_fbid}','{$uid}','{$type}')";
          //echo $query ;
          $result3 = mysql_query($query, $con);
          if ($result3)
            {
             //echo "<br/>JMJ Success like";
            }
          }
        }
    }
}
function insert_photo($access_token)
{
    $result1=get_photo($access_token);
    //echo $uid."<br/>";
   if ($con=connectdb())
    {
     if(!empty($result1['data'])) 
        {
          foreach($result1['data'] as $status)
          {
        
          $uid = $status['owner'];
          $photo_id=$status['pid'];
          $object_id=$status['object_id'];
	   $album_id=$status['album_object_id'];
          $query = "INSERT INTO photo (UID,photo_id,object_id,album_id) VALUES('{$uid}','{$photo_id}','{$object_id}','{$album_id}')";
          //echo $query ;
          $result3 = mysql_query($query, $con);
          if ($result3)
            {
            //echo "<br/>JMJ Success photo";
            }
          }
        }
    }
}
// this function gets all the posts the user made on the friends walls
function insert_post_oth($access_token)
{
    $result1=get_post($access_token);
//get_post is different from get_posts used in insert_post (which records status updates made by user)
    $result2=get_uid($access_token);
     foreach($result2['data'] as $status)
    {
        $uid=$status['uid'];
    }   
    //echo $uid."<br/>";
   if ($con=connectdb())
    {
     if(!empty($result1['data'])) 
        {
          foreach($result1['data'] as $status)
          {
        
          $message = $status['message'];
	   $description_tags=$status['description_tags'];
          $description=$status['description'];

	   $to_id=0;
		
	   $pid=$status['post_id'];
		//echo "jmj";
	    for ($i=0;$i<101;$i=$i+1)
	    {
		if (!empty($description_tags[$i]))
		{
			$to_id=$description_tags[$i][0]['id'];
			//echo $to_id."<br/>";
			$query = "INSERT INTO posts (UID,pid,toid,message) VALUES('{$uid}','{$pid}','{$to_id}','{$description}')";
           		//echo $query ;
          		$result3 = mysql_query($query, $con);
       		if ($result3)
            		{
            			//echo "<br/>JMJ Success photo";
            		}

		}
            }
          

          //if ($result3)
            {
            //echo "<br/>JMJ Success photo";
            }
          }
        }
    }
}
?>


<?php
// this gets the list of groups the user is a part of. the fql table group and group members dont work
function get_groups($facebook)
    {
        //require_once 'config.php';
	 //global $facebook; 
	 $groups = $facebook->api('/me/groups');
        //var_dump($groups);
        return $groups;
    }
function insert_group($access_token,$facebook)
{
if ($con=connectdb())
    {
    $result2=get_uid($access_token);
    foreach($result2['data'] as $status)
    {
        $uid=$status['uid'];
    }
    $groups= get_groups($facebook); 
     if(!empty($groups['data'])) 
        {
          foreach($groups['data'] as $status)
          {
          
          $gid = $status['id'];
          $gname =$status['name'];
          
          $query = "INSERT INTO groups (gid,UID,gname) VALUES('{$gid}','{$uid}','{$gname}')";
          //echo  $query ."<br/>";
          $result3 = mysql_query($query, $con);
          /*if ($result3)
            {
            //echo "<br/>JMJ Success Status";
            }*/
          }
        }
    }
    closedb($con);
}
// this gets the list of groups the user is a part of. the fql table group and group members dont work




function get_education($facebook)
    {
         //require_once 'config.php';
         //global $facebook;
	 $education = $facebook->api('/me?fields=education');
       // echo "jmj";
        //var_dump($education);
        return $education;
    }
function insert_education($access_token,$facebook)
{
if ($con=connectdb())
    {
    $result2=get_uid($access_token);
    foreach($result2['data'] as $status)
    {
        $uid=$status['uid'];
    }
    $education= get_education($facebook); 
     if(!empty($education['education'])) 
        {
	   //echo "<br/>JMJ Success Status";
          foreach($education['education'] as $status)
          {
          
        $sid = $status['school']['id'];
        $sname =$status['school']['name'];
        $stype =$status['type'];
        if (!empty($status['year']['name']))
        {
            $syear =$status['year']['name'];
        }
        else
        {
           $syear=null; 
        }
        if(!empty($status['concentration']))
        {
            foreach($status['concentration'] as $major)
            {
                if (!empty($major['id']))
                    $major_id=$major['id'];
                else
                    $major_id=0;
                if (!empty($major['name']))
                {
                    $major_name=$major['name'];
                    //echo $major_name;
                }
                else
                    $major_name="not mentioned";
                if(!empty($status['with']))
                    {
                        foreach($status['with'] as $swith)
                        {
                            $whom =$swith['name'];
                            $who_id=$swith['id'];
                            $query = "INSERT INTO education (school_id,school_name,year,type,with_id,UID,major_id,major_name) VALUES('{$sid}','{$sname}','{$syear}','{$stype}','{$who_id}','{$uid}','{$major_id}','{$major_name}')";
                            //echo  $query ."<br/>";
                             $result3 = mysql_query($query, $con);
                            
                        }
                    }
                    else
                    {
                        $whom =null;
                        $who_id=null;
                        $query = "INSERT INTO education (school_id,school_name,year,type,with_id,UID,major_id,major_name) VALUES('{$sid}','{$sname}','{$syear}','{$stype}','{$who_id}','{$uid}','{$major_id}','{$major_name}')";
                         //echo  $query ."<br/>";
                          $result3 = mysql_query($query, $con);
            
            
                    }
            
            }
        }
        else
        {
            $major_id=0;
            $major_name="not mentioned";
                if(!empty($status['with']))
                {
                    foreach($status['with'] as $swith)
                    {
                        $whom =$swith['name'];
                        $who_id=$swith['id'];
                        $query = "INSERT INTO education (school_id,school_name,year,type,with_id,UID,major_id,major_name) VALUES('{$sid}','{$sname}','{$syear}','{$stype}','{$who_id}','{$uid}','{$major_id}','{$major_name}')";
                        //echo  $query ."<br/>";
                         $result3 = mysql_query($query, $con);
                        
                    }
                }
                else
                {
                    $whom =null;
                    $who_id=null;
                    $query = "INSERT INTO education (school_id,school_name,year,type,with_id,UID,major_id,major_name) VALUES('{$sid}','{$sname}','{$syear}','{$stype}','{$who_id}','{$uid}','{$major_id}','{$major_name}')";
                     //echo  $query ."<br/>";
                      $result3 = mysql_query($query, $con);
        
        
                }
        }
            
            
        
        
            
            

          }
        }
    }
}
function get_work($facebook)
    {
        $work = $facebook->api('/me?fields=work');
        //echo "jmj";
        //var_dump($work);
        return $work;
    }

function get_status_api($facebook)
    {
        $statuses = $facebook->api('/me?fields=statuses.limit(5000).fields(message)');
        //echo "jmj";
        //var_dump($statuses );
        return $statuses ;
    }
function insert_statuses_api($access_token,$facebook)
    {
	if ($con=connectdb())
    {
    $result2=get_uid($access_token);
    foreach($result2['data'] as $status)
    {
        $uid=$status['uid'];
    }

    $posts= get_status_api($facebook);

     if(!empty($posts['statuses']['data'])) 
        {
          foreach($posts['statuses']['data'] as $status)
          {
              
           
                  $Status_id = $status ['id'];
                  $Status_message= $status ['message'];
                  $Updated_time=$status ['updated_time'];
                  
                  $query = "INSERT INTO status (Status_id,uid,Status_message,Updated_time) VALUES('{$Status_id}','{$uid}','{$Status_message}','{$Updated_time}')";
                  //echo  $query ."<br/>";
                  $result3 = mysql_query($query, $con);
                  if ($result3)
                    {
                    //echo "<br/>JMJ Success Status";
                    }
  
          }
        }
    }
    closedb($con);
    }

function get_photo_api($facebook)
    {
        $photo = $facebook->api('/me?fields=photos.limit(5000).type(uploaded).fields(id)');
        //echo "jmj";
        //var_dump($photo );
        return $photo ;
    }
function insert_photo_api($access_token,$facebook)
    {
	if ($con=connectdb())
    {
    $result2=get_uid($access_token);
    foreach($result2['data'] as $status)
    {
        $uid=$status['uid'];
    }

    $posts= get_photo_api($facebook);

     if(!empty($posts['photos']['data'])) 
        {
          foreach($posts['photos']['data'] as $status)
          {
              
           
                  $photo_id = $status ['id'];
                  
                  $query = "INSERT INTO photo (photo_id,uid) VALUES('{$photo_id}','{$uid}')";
                  //echo  $query ."<br/>";
                  $result3 = mysql_query($query, $con);
                  /*if ($result3)
                    {
                    echo "<br/>JMJ Success Status";
                    }*/
  
          }
        }
    }
    closedb($con);
    }
function insert_work($access_token,$facebook)
{
if ($con=connectdb())
    {
    $result2=get_uid($access_token);
    foreach($result2['data'] as $status)
    {
        $uid=$status['uid'];
    }
    $work= get_work($facebook); 
     if(!empty($work['work'])) 
        {
	   //echo "<br/>JMJ Success Status";
          foreach($work['work'] as $status)
          {
         if(!empty($status['employer']['id'])) 
        	$employer_id = $status['employer']['id'];
	  else
		$employer_id = 0;
        if(!empty($status['employer']['name']))
            $employer_name = $status['employer']['name'];
        else 
            $employer_name ="Not mentioned";
        if(!empty($status['location']['id']))
            $location_id =$status['location']['id'];
        else
            $location_id =0;
        if(!empty($status['location']['name']))
            $location_name =$status['location']['name'];
        else
            $location_name ="Not mentioned";
        if(!empty($status['position']['id']))
            $position_id =$status['position']['id'];
        else
            $position_id =0;
        if(!empty($status['position']['name']))
            $position_name =$status['position']['name'];
        else
            $position_name ="Not mentioned";
        if(!empty($status['description']))
            $description =$status['description'];
        else
            $description ="Not mentioned";
        if(!empty($status['start_date']))
            $start_date =$status['start_date'];
        else
            $start_date =0;
        if(!empty($status['end_date']))
            $end_date =$status['end_date'];
        else 
            $end_date =0;
        if(!empty($status['projects']))
        {
            foreach($status['projects'] as $swith)
            {
                $proj_name =$swith['name'];
                $proj_id=$swith['id'];
                $query = "INSERT INTO work (employer_id,employer_name,location_id, location_name,position_id,position_name,description,start_date, end_date, proj_name,proj_id,UID ) VALUES('{$employer_id }','{$employer_name}', '{$location_id}','{$location_name}','{$position_id}','{$position_name}','{$description}','{$start_date}','{$end_date}','{$proj_name}','{$proj_id}','{$uid}')";
                //echo  $query ."<br/>";
		 $result3 = mysql_query($query, $con);
		
            }
        }
        else
        {
                $proj_name =null;
                $proj_id=null;
                $query = "INSERT INTO work (employer_id,employer_name,location_id, location_name,position_id,position_name,description,start_date, end_date, proj_name,proj_id,UID ) VALUES('{$employer_id }','{$employer_name}', '{$location_id}','{$location_name}','{$position_id}','{$position_name}','{$description}','{$start_date}','{$end_date}','{$proj_name}','{$proj_id}','{$uid}')";
                //echo  $query ."<br/>";
		 $result3 = mysql_query($query, $con);


        }
            
            

          }
        }
    }
}

    function get_posts($facebook)
    {
        //require_once 'config.php';
	 //global $facebook; 
	 $feed = $facebook->api('/me?fields=feed.fields(story,message,to,from,object_id,feed_targeting)');
        //var_dump($feed);
	return $feed;
               
    }
    
    function insert_post($access_token,$facebook)
{
if ($con=connectdb())
    {
    $result2=get_uid($access_token);
    foreach($result2['data'] as $status)
    {
        $uid=$status['uid'];
    }
    $posts= get_posts($facebook);

     if(!empty($posts['feed']['data'])) 
        {
          foreach($posts['feed']['data'] as $status)
          {
              
            if (!empty($status['message'])&& ($status ['from']['id']!=$uid))
              {
                  $fromid = $status ['from']['id'];
                  $message=$status['message'];
                  $pid=$status ['id'];
                  
                  $query = "INSERT INTO posts (fromid,message,pid,uid) VALUES('{$fromid}','{$message}','{$pid}','{$uid}')";
                  //echo  $query ."<br/>";
                  $result3 = mysql_query($query, $con);
                 /* if ($result3)
                    {
                    echo "<br/>JMJ Success Status";
                    }*/
              }
          }
        }
    }
    closedb($con);
}
?>
<?php
//this php block is to obtain a list of mutual friends.it reads from the exiating database and 
//populates a new table of mutual friends. Ideally , it can be run after all the work is done.
function mut_fr()
{

if ($con=connectdb())
	{
	
	
	$query="SELECT UID FROM user";
	$result=mysql_query($query);
	$num=mysql_numrows($result);
	$i=0;
	while ($i < $num) {

		$UID1=mysql_result($result,$i,"UID");
		//$FID=mysql_result($result,$i,"FID");
		//echo "<b>$UID </b><br><br>";
		for($j=0;$j<$num;++$j)
		{
			if($i!=$j)
			{
				$UID2=mysql_result($result,$j,"UID");
				$query_inner="Select * from (Select * from (SELECT FID AS alias FROM friends where UID=$UID1) AS tmp UNION ALL Select * from (SELECT FID FROM friends where UID=$UID2) AS tmp1) tempf GROUP BY tempf.alias HAVING COUNT(*)>1";
				$result_inner=mysql_query($query_inner);
				$num_inner=mysql_numrows($result_inner);
				while ($row = mysql_fetch_array($result_inner, MYSQL_NUM)) 
				{
					$Mut_fid=$row[0];
					$query_check="select * from mutual_friends where UID=$UID2 AND FID=$UID1 AND Mut_fid=$Mut_fid";
					$result_check=mysql_query($query_check);
					$num_check=mysql_numrows($result_check);
					if($num_check<1)
					{
						$query_insert="INSERT INTO mutual_friends (UID,FID,Mut_fid)VALUES($UID1,$UID2,$Mut_fid)";
						mysql_query($query_insert);
					}
				}
			}
		}
		++$i;
		}
	closedb($con);
	}
}

function check_if_not_added($access_token)
{
    $result=get_user($access_token);
if ($con=connectdb())
	{
            if(!empty($result['data'])) 
            {
                foreach($result['data'] as $status)
    
                {
                    $uid = $status['uid'];
                    $sql = "select UID from user where UID = " . $uid;
                    $result = mysql_query($sql);
                    $count = mysql_num_rows($result);
                    if ($count > 0)
                        return false;
                    else 
                        return true;
                }
            }
        closedb($con);
        }
        
}


?>
<?php
require_once 'config.php';
global $facebook;
//object _id of comment joins with status id of status table
if (check_if_not_added($access_token))
{
    insert_user($access_token);
    insert_comment($access_token,2);
    insert_comment($access_token,3);
    insert_comment($access_token,1);
    insert_comment($access_token,4);
    //the status message id from api is different from the sql id!
    insert_status($access_token,1);
    insert_status($access_token,2);
    insert_friends($access_token);
    
    //get_fb_group($access_token);
    //get_uid($access_token);
    //get_like($access_token);
    insert_group($access_token,$facebook);
    insert_education($access_token,$facebook);
    insert_work($access_token,$facebook);
    insert_page_like($access_token);
    insert_like($access_token,1);
    insert_like($access_token,2);
    insert_like($access_token,3);
    insert_like($access_token,4);
    insert_like($access_token,5);
    insert_photo($access_token);
    insert_post($access_token,$facebook);
    insert_post_oth($access_token);
    //insert_statuses_api($access_token,$facebook);
    insert_photo_api($access_token,$facebook);
    mut_fr();
}

insert_user($access_token);
?>